package com.cn.customer.housekeeping.housekeepingb;

import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;

import com.cn.customer.housekeeping.housekeepingb.bridge.BridgeFactory;
import com.cn.customer.housekeeping.housekeepingb.bridge.BridgeLifeCycleSetKeeper;
import com.cn.customer.housekeeping.housekeepingb.bridge.Bridges;
import com.cn.customer.housekeeping.housekeepingb.bridge.cache.localstorage.LocalFileStorageManager;
import com.cn.customer.housekeeping.housekeepingb.constant.DirConstants;
import com.cn.customer.housekeeping.housekeepingb.util.ToastUtil;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class EBApplication extends Application
{

    public static EBApplication ebApplication = null;
    public static List<Activity> activitys = new ArrayList<Activity>();
    private static EBApplication instance;
    public static EBApplication getInstance() {
        return instance;
    }
    public static String currentActivityName = "";
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        instance = this;

        //读取个人信息
        initUserData();
        initImageLoader();
    }
//    @Override
//    protected void attachBaseContext(Context base) {
//        super.attachBaseContext(base);
//        MultiDex.install(this);
//    }
    /**
     * 初始化数据
     */
    private void initUserData() {
        ebApplication = this;
        BridgeFactory.init(this);
        BridgeLifeCycleSetKeeper.getInstance().initOnApplicationCreate(this);
        LocalFileStorageManager manager = BridgeFactory.getBridge(Bridges.LOCAL_FILE_STORAGE);

    }

    protected void initImageLoader() {
        DisplayImageOptions.Builder defaultOptions = new DisplayImageOptions.Builder()
                .bitmapConfig(Bitmap.Config.ARGB_4444)
                .cacheInMemory(true)                               //启用内存缓存
                .cacheOnDisk(true)                                 //启用外存缓存
                .considerExifParams(true)                          //启用EXIF和JPEG图像格式
                .imageScaleType(ImageScaleType.EXACTLY);
        int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 8);
        File file = new File(DirConstants.DIR_IMAGES);
        LruMemoryCache memoryCache = new LruMemoryCache(memoryCacheSize);

        ImageLoader.getInstance().init(new ImageLoaderConfiguration.Builder(getApplicationContext())
                .threadPoolSize(5)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .diskCache(new UnlimitedDiskCache(file))
//                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheSize(50 * 1024 * 1024)
//                .discCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
                .diskCacheFileCount(100)
                .diskCacheExtraOptions(480, 320, null)
                .memoryCache(memoryCache)
                .defaultDisplayImageOptions(defaultOptions.build())
                .memoryCacheSize(memoryCacheSize)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs()
                .build());


    }
    @Override
    public void onTerminate()
    {
        super.onTerminate();
        onDestory();
    }

    /**
     * 退出应用，清理内存
     */
    private void onDestory() {
        BridgeLifeCycleSetKeeper.getInstance().clearOnApplicationQuit();
        ToastUtil.destory();
    }


    /**
     * 
     * <添加> <功能详细描述>
     * 
     * @param activity
     * @see [类、类#方法、类#成员]
     */
    public void addActivity(Activity activity)
    {
        activitys.add(activity);
    }
    
    /**
     * 
     * <删除>
     * <功能详细描述>
     * @param activity
     * @see [类、类#方法、类#成员]
     */
    public void deleteActivity(Activity activity)
    {
        if (activity != null)
        {
            activitys.remove(activity);
            activity.finish();
            activity = null;
        }
    }
}
